//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <libpub.h>
#include <bo_cmds.h>

//==========================================================================

//Global variable
tCommandTbl *Do_MainCmdTbl = NULL;
tInitTbl *Do_MainInitTbl = NULL;
tCommandTbl *Do_TestCmdTbl = NULL;
tAliasTbl *Do_AliasTbl = NULL;
unsigned long uLastError = 0;

extern unsigned long __initcmds_start;
extern unsigned long __maincmds_start;
extern unsigned long __testcmds_start;

void Do_InitDevices(void)
{
#if defined(_GNUC)
    Do_MainInitTbl = (tInitTbl *)&__initcmds_start;
    Do_MainCmdTbl = (tCommandTbl *)&__maincmds_start;
    Do_TestCmdTbl = (tCommandTbl *)&__testcmds_start;
    uLastError = 0;
#endif

    TOUT(1, 1, printf("Do Init Devices (0x%x)\n", Do_MainInitTbl));

    tInitTbl *p = Do_MainInitTbl;
    int i = 0;
    while(p->init) {
        int ret = p->init();
        if (ret) {
            printf("FUNC[%d][%x], Init %s fail.\n", i, p->init, p->desc);
        }
        i++;p++;
    }
}

//==========================================================================
int Do_Alias(char *param)
{
    printk("\nDumping all defined aliases:\n");
    tAliasTbl *pTbl = Do_AliasTbl;
    while(*pTbl->alias) {
        printk("%s\t\t=%s\n", pTbl->alias, pTbl->cmd);
        pTbl++;
    }
    return 0;
}

int Do_Help(char *param)
{
    TOUT(1, 1, printf("do help [%s]\n", param));
    tCommandTbl *pCmdTbl = Do_MainCmdTbl;

    if (*param) {
        while(pCmdTbl->cmd) {
            if (0 <= Bo_MatchString(pCmdTbl->usage, param)) {
                printk("Usage: %s\n%s\n", pCmdTbl->usage, pCmdTbl->help);
                break;
            }
            pCmdTbl++;
        }

        //Search alias list
        if (!pCmdTbl->cmd) {
            tAliasTbl *pTbl = Do_AliasTbl;
            while(*pTbl->alias) {
                if (0 <= Bo_MatchString(pTbl->alias, param)) {
                    printk("%s\t\t=%s\nAlias Help: %s\n", pTbl->alias, pTbl->cmd, pTbl->help);
                    break;
                }
                pTbl++;
            }
        }
    }
    else {
        while(pCmdTbl->cmd) {
            printk("%s\n", pCmdTbl->usage);
            pCmdTbl++;
        }

        Do_Alias("");
    }
    return 0;
}

int Do_Version(char *param)
{
    TOUT(0, 1, printf("do version [%s]\n", param));
    printf( "\n\tElastos2.1 Bootloader\n\tVersion: %s For %s, Time Stamp: %s\n",
        BOOTLOADVERSION, Xo_GetTargetBoard(), COMPILE_TIME_STAMP );
    return 0;
}

DECL_MAINCMD(Do_Help, "help {command}  - show detail help information for 'command'",  \
    "'help' prints online help .\n\n"   \
    "Without arguments, it prints a short usage message for all commands.\n\n"  \
 	"To get detailed help information for specific commands you can type\n" \
    "'help' with one or more command names as arguments.\n");

DECL_MAINCMD(Do_Version, "version    - show version",  \
    "print version on console, version format is xx.xx.xx\n");

DECL_MAINCMD(Do_Alias, "alias    - show all alias list",  \
    "...\n");

